package com.autumn.platform.dao.component.impl;

import java.io.InputStream;
import java.util.Map;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.builder.SqlSourceBuilder;
import org.apache.ibatis.builder.xml.XMLConfigBuilder;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.builder.xml.XMLStatementBuilder;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.parsing.XNode;
import org.apache.ibatis.scripting.xmltags.XMLScriptBuilder;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;

import com.autumn.platform.dao.component.IMybatisComponentFactory;
import com.autumn.platform.dao.expression.ExpressionParameterHandler;
import com.autumn.platform.dao.schema.script.SchemaXMLScriptBuilder;
import com.autumn.platform.dao.schema.script.sqlsource.SchemaSqlSourceBuilder;

public class DefaultMybatisComponentFactory implements IMybatisComponentFactory {

    @Override
    public SqlSessionFactoryBuilder newSqlSessionFactoryBuilder() {
        return new SqlSessionFactoryBuilder();
    }

    @Override
    public XMLConfigBuilder newXMLConfigBuilder(InputStream inputStream, String environment, Properties props) {
        return new XMLConfigBuilder(inputStream, environment, props);
    }

    @Override
    public XMLMapperBuilder newXMLMapperBuilder(InputStream inputStream, Configuration configuration, String resource,
            Map<String, XNode> sqlFragments) {
        return new XMLMapperBuilder(inputStream, configuration, resource, sqlFragments);
    }

    @Override
    public XMLStatementBuilder newXMLStatementBuilder(Configuration configuration, MapperBuilderAssistant builder, XNode node, String requiredDatabaseId) {
        return new XMLStatementBuilder(configuration, builder, node, requiredDatabaseId);
    }

    @Override
    public XMLScriptBuilder newXMLScriptBuilder(Configuration configuration, XNode node, Class<?> parameterType) {
        return new SchemaXMLScriptBuilder(configuration, node, parameterType);
    }

    @Override
    public SqlSourceBuilder newSqlSourceBuilder(Configuration configuration) {
        return new SchemaSqlSourceBuilder(configuration);
    }

    @Override
    public Configuration newConfiguration() {
        return new Configuration();
    }

    @Override
    public Environment newEnvironment(String id, TransactionFactory transactionFactory, DataSource dataSource) {
        return new Environment(id, transactionFactory, dataSource);
    }

    @Override
    public TransactionFactory newTransactionFactory() {
        return new SpringManagedTransactionFactory();
    }

    @Override
    public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
        return new ExpressionParameterHandler(mappedStatement, parameterObject, boundSql);
    }
}
